package org.ilc.message;

import org.ilc.push.IDataProvider;
import org.ilc.push.ISendTask;

import java.util.Timer;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


public class MessageQueue {
	private int sleepTime = 3000;


	//定时器
	private Timer timer;

	private ThreadPoolExecutor threadPool=null;
	/**
	 * 数据提供
	 */
	private IDataProvider dataProvider;

    private ISendTask sendTask;
	
	private QueueTimer queueTimer=null;
	/**
	 * 
	 * @param dataProvider
	 * @param minThreads 消息处理线程池最小线程
	 * @param maxThreads 消息处理线程池最大线程
	 * @param sleepTime
	 */
	public MessageQueue(IDataProvider dataProvider, ISendTask sendTask,int minThreads,int maxThreads,int sleepTime){
		this.sleepTime=sleepTime;

		this.threadPool = new ThreadPoolExecutor(minThreads, maxThreads, 0L,
				TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
		this.dataProvider=dataProvider;
	    this.sendTask = sendTask;
	}


	/**
	 * 启动
	 */
	public synchronized void start() {

		
		if(null!=queueTimer && queueTimer.isRunning()){
			return;
		}
		queueTimer=new QueueTimer (new QueueExecutor(this.threadPool),this.dataProvider,this.sendTask,this.sleepTime);
		
		queueTimer.startScan();
	}

	/**
	 * 停止
	 */
	public synchronized void stop() {
		if(null!=this.queueTimer){
			this.queueTimer.stopScan();
		}
	}

    protected ISendTask getSendTask() {
        return this.sendTask;
    }
}
